package com.hoopchina.util.xml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.os.Handler;
import android.os.Message;

public class InputStream2XML {
     //把流写进文件
    public static boolean start( InputStream aInputStream, String aPath, String aFileName ) {
        try {
            File targetFile = new File( aPath, aFileName );
            OutputStream out = new FileOutputStream( targetFile );
            byte buf[] = new byte[1024];
            int len;
            while ( ( len = aInputStream.read( buf ) ) > 0 )
                out.write( buf, 0, len );
            out.close();
            aInputStream.close();
            return true;
        }
        catch ( IOException e ) {
            return false;
        }
    }

    public static void startAsync( final InputStream aInputStream, final String aPath,
            final String aFileName, final Handler aHandler ) {

        new Thread() {
            public void run() {
                File targetFile = null;
                Message msg = null;
                try {
                    msg = aHandler.obtainMessage();
                    targetFile = new File( aPath, aFileName );
                    OutputStream out = new FileOutputStream( targetFile );
                    byte buf[] = new byte[1024];
                    int len;
                    while ( ( len = aInputStream.read( buf ) ) > 0 )
                        out.write( buf, 0, len );
                    out.close();
                    aInputStream.close();
                }
                catch ( IOException e ) {}
                finally {
                    msg.obj = targetFile;
                    aHandler.sendMessage( msg );
                }
            }
        }.start();
    }
}